﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Article;
using DataObject.Category;

public partial class Controls_Common_Header : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltProduct.Text = ListCategoryProduct();
            //ltArticle.Text = ListCategoryArticle();
            //ltIntro.Text = ArticleIntro();    
        }       
    }

    private string ArticleIntro()
    {
        var sb = new StringBuilder();
        try
        {
            var categoryId = ConvertUtility.ToInt32(WebConfigurationManager.AppSettings["StaticIntro"]);
            var o = ArticleController.Article_GetListByCategoryId(categoryId);
            if (o != null)
            {
                sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", UrlHelpers.ArticleStatic((int)StaticType.Article, o[0].ArticleId, o[0].Title), o[0].Title);
                return sb.ToString();
            }
        }
        catch { }

        return string.Empty;
    }

    private string ListCategoryProduct()
    {
        var sb = new StringBuilder();

        var categories = CategoryController.Category_GetByParentId((int)CategoryType.Product, Constants.PARENT_ROOT, Constants.ACTIVE);
        if (categories.Count > 0)
        {
            if (categories.Count > 0)
            {
                sb.Append("<ul class=\"sub-menu\">");
                for (int i = 0; i < categories.Count; i++)
                {
                    sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>",
                                            UrlHelpers.ListProductByCategory(categories[i].CategoryId, categories[i].Name),
                                            categories[i].Name);
                }
                sb.Append("</ul>");
            }
        }
        return sb.ToString();
    }

    private string ListCategoryArticle()
    {
        var categories = CategoryController.Category_GetByParentId_Filter(false, (int)CategoryType.Article, Constants.PARENT_ROOT, Constants.ACTIVE);
        if (categories.Count > 0)
        {
            var sb = new StringBuilder();

            //sb.Append("<li><a href=\"#\">Tin tức</a>");
            if (categories.Count > 0)
            {
                sb.Append("<ul>");
                for (int i = 0; i < categories.Count; i++)
                {
                    sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>",
                                            UrlHelpers.ListArticleByCategory(categories[i].CategoryId, categories[i].Name),
                                            categories[i].Name);
                    /*
                    var subCategory = CategoryController.Category_GetByParentId_Filter(false, (int)CategoryType.Article, categories[i].CategoryId, Constants.ACTIVE);
                    if (subCategory.Count > 0)
                    {
                        sb.AppendFormat("<li><a href=\"{0}\">{1}</a>",
                                            UrlHelpers.ListArticleByCategory(categories[i].CategoryId, categories[i].Name),
                                            categories[i].Name);
                        sb.Append("<ul>");
                        for (int j = 0; j < subCategory.Count; j++)
                            sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>",
                                                UrlHelpers.ListArticleByCategory(categories[i].CategoryId, subCategory[j].Name),
                                                subCategory[j].Name);
                        sb.Append("</ul>");
                        sb.Append("</li>");
                    }
                    else
                    {
                        sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>",
                                            UrlHelpers.ListArticleByCategory(categories[i].CategoryId, categories[i].Name),
                                            categories[i].Name);
                    }
                    */
                }
                sb.Append("</ul>");
                //sb.Append("</li>");
            }
            //else
            //    sb.Append("<li><a href=\"#\">Tin tức</a></li>");

            return sb.ToString();
        }
        return string.Empty;
    }

    //protected void hplSearch_Click(object sender, EventArgs e)
    //{
    //    var keyword = HttpUtility.UrlEncode(txtKeyword.Text);
    //    Response.Redirect(UrlHelpers.SearchProduct(keyword));
    //}
}